﻿using System;
using System.Linq;
using System.ComponentModel;
using System.Windows.Data;
using System.Collections.ObjectModel;

using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Prism.Commands;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;

using PortfolioModule.Models;
using PortfolioModule.Services;
using ServiceAgent;
using Infrastructure;
using Infrastructure.Helpers;
using Infrastructure.Events;
using System.Windows.Input;
using System.Windows;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Prism.Interactivity.InteractionRequest;
using PortfolioModule.Views;
using ServiceAgent.StudentService;
using System.Collections.Generic;
using ServiceAgent.CatalogueService;
using ServiceAgent.SystemService;
using ServiceAgent.PortfolioService;

namespace PortfolioModule.ViewModels
{
    /// <summary>
    /// ViewModel for GanHoSoViewModel.
    /// </summary>
    public class GanHoSoViewModel : NotificationObject, INavigationAware
    {
        #region Fields
        private IRegionNavigationService navigationService;
        private IEventAggregator eventAggregator;

        private readonly IUnityContainer container;
        private IRegionManager regionManager;

        ObservableCollection<ServiceAgent.CatalogueService.DonViDTO> donViList;
        ServiceAgent.CatalogueService.DonViDTO currentDonVi;


        ObservableCollection<LoaiHoSoDTO> loaiHoSoList;
        ObservableCollection<LoaiHoSoDTO> loaiHoSoDonViList = new ObservableCollection<LoaiHoSoDTO>();
        
        ObservableCollection<LoaiHoSoDTO> oldLoaiHoSoDonViList;
        #endregion

        #region Constructor
        /// <summary>
        /// Default constructor.
        /// </summary>
        public GanHoSoViewModel(IEventAggregator eventAggregator, IRegionManager regionManager, IUnityContainer container)
        {
            this.eventAggregator = eventAggregator;
            this.regionManager = regionManager;
            this.container = container;

            this.Initialize();
        }
        #endregion

        #region Administrative Properties
        public ObservableCollection<ServiceAgent.CatalogueService.DonViDTO> DonViList
        {
            get { return donViList; }
            set
            {
                donViList = value;
                RaisePropertyChanged(() => this.DonViList);
            }
        }

        public ServiceAgent.CatalogueService.DonViDTO CurrentDonVi
        {
            get { return currentDonVi; }
            set
            {
                currentDonVi = value;
                RaisePropertyChanged(() => this.CurrentDonVi);
                this.LoaiHoSoDonViList.Clear();
                this.GetLoaiHoSoList(CurrentDonVi.ID);
                this.GetLoaiHoSoDonViList(CurrentDonVi.ID);
                this.SaveCommand.RaiseCanExecuteChanged();
            }
        }

        public ObservableCollection<LoaiHoSoDTO> LoaiHoSoList
        {
            get { return loaiHoSoList; }
            set { loaiHoSoList = value;
            RaisePropertyChanged(() => this.LoaiHoSoList);
            }
        }

        public ObservableCollection<LoaiHoSoDTO> LoaiHoSoDonViList
        {
            get { return loaiHoSoDonViList; }
            set { loaiHoSoDonViList = value;
            RaisePropertyChanged(() => this.LoaiHoSoDonViList);
            }
        }
        #endregion

        #region Command Properties
        public DelegateCommand<object> GoBackCommand { get; private set; }
        public DelegateCommand SaveCommand { get; private set; }
        #endregion

        #region Private Methods
        private void Initialize()
        {
            this.GoBackCommand = new DelegateCommand<object>(GoBack, CanGoBack);
            this.SaveCommand = new DelegateCommand(this.OnRaiseSave, this.CanRaiseSave);
            this.GetDonViList();
        }

        void GetDonViList()
        {
            try
            {
                using (var proxy = container.Resolve<CatalogueServiceAgent>())
                {
                    DonViList = proxy.GetDonViListByCapDonVi();
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetLoaiHoSoList(int DonViID)
        {
            try
            {
                using (var proxy = container.Resolve<PortfolioServiceAgent>())
                {
                    LoaiHoSoList = proxy.GetLoaiHoSoByNotYetDonViID(DonViID);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        void GetLoaiHoSoDonViList(int id)
        {
            try
            {
                using (var proxy = container.Resolve<PortfolioServiceAgent>())
                {
                    oldLoaiHoSoDonViList = proxy.GetLoaiHoSoByDonViID(id);
                }
                foreach (var quyen in oldLoaiHoSoDonViList)
                {
                    loaiHoSoDonViList.Add(quyen);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        private void OnRaiseSave()
        {
            try
            {
                var toRemove = oldLoaiHoSoDonViList.Except(loaiHoSoDonViList);
                var toAdd = loaiHoSoDonViList.Except(oldLoaiHoSoDonViList);

                if (toAdd.Count() > 0)
                {
                    using (var proxy = container.Resolve<PortfolioServiceAgent>())
                    {
                        proxy.SetLoaiHoSoListForDonVi(CurrentDonVi.ID, new ObservableCollection<LoaiHoSoDTO>(toAdd));
                    }
                }

                if (toRemove.Count() > 0)
                {
                    using (var proxy = container.Resolve<PortfolioServiceAgent>())
                    {
                        proxy.UnSetLoaiHoSoListForDonVi(CurrentDonVi.ID, new ObservableCollection<LoaiHoSoDTO>(toRemove));
                    }
                }
                oldLoaiHoSoDonViList = loaiHoSoDonViList;
                MessageBox.Show("Lưu lại thành công");
            }
            catch (Exception)
            {
                MessageBox.Show("Có lỗi xảy ra");
            }
        }

        private bool CanRaiseSave()
        {
            return this.CurrentDonVi != null;
        }

        private void GoBack(object commandArg)
        {
            if (navigationService.Journal.CanGoBack)
            {
                navigationService.Journal.GoBack();
            }
        }

        private bool CanGoBack(object commandArg)
        {
            return navigationService.Journal.CanGoBack;
        }
        #endregion

        #region INavigationAware Members
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            // Called to see if this view can handle the navigation request.
            // If it can, this view is activated.
            return false;
        }

        public void OnNavigatedFrom(NavigationContext navigationContext)
        {

            // Called when this view is deactivated as a result of navigation to another view.
        }

        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            //// Called to initialize this view during navigation.

            // getting a reference to navigation service:

            //// Retrieve any required paramaters from the navigation Uri.

        }
        #endregion
    }
}
